/**
 * 显示菜单
 */
function showMenu () {
  const prompt = require('@system.prompt')
  const router = require('@system.router')
  const appInfo = require('@system.app').getInfo()
  prompt.showContextMenu({
    itemList: ['保存桌面', '浏览历史', '取消'],
    success: function (ret) {
      switch (ret.index) {
      case 0:
        // 保存桌面
        createShortcut()
        break
      case 1:
        // 关于
        router.push({
          uri: '/About',
          params: {
            name: appInfo.name,
            icon: appInfo.icon
          }
        })
        break
      case 2:
        // 取消
        break
      default:
        prompt.showToast({
          message: 'error'
        })
      }
    }
  })
}

/**
 * 创建桌面图标
 * 注意：使用加载器测试`创建桌面快捷方式`功能时，请先在`系统设置`中打开`应用加载器`的`桌面快捷方式`权限
 */
function createShortcut () {
  const prompt = require('@system.prompt')
  const shortcut = require('@system.shortcut')
  shortcut.hasInstalled({
    success: function (ret) {
      if (ret) {
        prompt.showToast({
          message: '已创建桌面图标'
        })
      } else {
        shortcut.install({
          success: function () {
            prompt.showToast({
              message: '成功创建桌面图标'
            })
          },
          fail: function (errmsg, errcode) {
            prompt.showToast({
              message: `${errcode}: ${errmsg}`
            })
          }
        })
      }
    }
  })
}

function Ajax(URL) {
    const fetch = require('@system.fetch')
    return new Promise ((resolve,reject) => {
        fetch.fetch({
            url: URL,
            success: function (response) {
                resolve(response)
                console.log(`the status code of the response: ${response.code}`)
                console.log(`the data of the response: ${response.data}`)
                console.log(`the headers of the response: ${JSON.stringify(response.headers)}`)
            },
            fail: function (err, code) {
                reject('err')
                console.log(`handling fail, code = ${code}`)
            }
        })
    })
}

function Usernetwork () {
    const network = require('@system.network')
    return new Promise ((resolve,reject) => {
        network.getType({
            success: function (data) {
                resolve(data)
                console.log(`handling success: ${data.type}`)
            }
        })
    })
}

function Alert(val) {
    const prompt = require('@system.prompt')
    prompt.showToast({
        message: val
    })
}

function getLocal (name) {
    const storage = require('@system.storage')
    return new Promise ((resolve, reject) => {
        storage.get({
            key: name,
            default: 'no',
            success: function (data) {
                if (data == 'no') {
                    resolve(123)
                } else {
                    resolve(JSON.parse(data))
                }
            },
            fail: function (data, code) {
                reject(2233)
            }
        })
    })
}

function setLocal (name, val) {
    const storage = require('@system.storage')
    return new Promise((resolve, reject) => {
        storage.set({
            key: name,
            value: val,
            success: function (data) {
                resolve()
            },
            fail: function (data, code) {
                reject(data)
            }
        })
    })
}

function clearLocal () {
    const storage = require('@system.storage')
    return new Promise ((resolve, reject) => {
        storage.clear({
            success: function (data) {
                resolve()
            },
            fail: function (data, code) {
                reject(data)
            }
        })
    })
}

export default {
  showMenu,
  createShortcut,
  Ajax,
  Usernetwork,
  Alert,
  getLocal,
  setLocal
}
